#!/bin/sh

. /usr/share/libubox/jshn.sh
. /lib/functions.sh

bridge_json_init()
{
    json_init
    json_add_string "equipmentType" "01"
    #添加基础数据
    json_add_object "basicData"
    json_select ..
    #添加状态数据
    json_add_object "statusData"
    json_select ..
    #添加远程设备数据
    json_add_object "connectionInfo"
    json_select ..
}

get_bridge_info_oid()
{
    bridge_json_init
    json_add_string "equipmentNumber" "YH800kV2241WXWQD"
    json_add_string "equipmentType" "01"

    local filename="/bin/bridge_tmp"
    local lineno=0
    while IFS= read -r line
    do
        local res=$(snmpwalk -c public -v 1 $1 $line | tr -d '"');
        match=$(echo "$res"|awk -F '=' '{print $2}'|awk -F: '{for(i=2; i<=NF; i++) printf "%s%s", $i, (i<NF?FS:RS)}'|awk '{gsub(/^[[:space:]]*|[[:space:]]*$/, ""); print}')
        case $lineno in
        0)
            json_select basicData
            case $match in
            1)
                json_add_string "deviceMode" "bridge"
                ;;
            2)
                json_add_string "deviceMode" "router"
                ;;
            3)
                json_add_string "deviceMode" "soho"
                ;;
            *)
                json_add_string "deviceMode" "unknown"
                ;;
            esac
            ;;
        1)
            json_select basicData
            case $match in
            1)
                json_add_string "wirelessMode" "sta"
                ;;
            2)
                json_add_string "wirelessMode" "ap"
                ;;
            3)
                json_add_string "wirelessMode" "aprepeater"
                ;;
            4)
                json_add_string "wirelessMode" "apwds"
                ;;
            esac
            ;;
        2)
            json_select basicData
            json_add_string "channelWidth" "$match"
            ;;
        3)
            json_select basicData
            json_add_string "antennaGain" "$match"
            ;;
        4)
            json_select basicData
            json_add_string "pointDistance" "$match"
            ;;
        5)
            json_select basicData
            json_add_string "accessPointsNumber" "$match"
            ;;
        6)
            json_select basicData
            json_add_string "encryptionMethod" "$match"
            ;;
        7)
            json_select basicData
            json_add_string "ssid" "$match"
            ;;
        8)
            json_select basicData
            json_add_string "model" "$match"
            ;;
        9)
            json_select basicData
            json_add_string "mac" "$match"
            ;;
        10)
            json_select statusData
            json_add_string "workingFrequency" "$match"
            ;;
        11)
            json_select statusData
            json_add_string "transmitPower" "$match"
            ;;
        12)
            json_select statusData
            json_add_string "signalStrength" "$match"
            ;;
        13)
            json_select statusData
            json_add_string "backgroundNoise" "$match"
            ;;
        14)
            json_select statusData
            json_add_string "polarizationSignalDifference" "$match"
            ;;
        15)
            json_select "statusData"
            json_add_string "transmissionCapacity" "$match"
            ;;
        16)
            json_select statusData
            json_add_string "receptionThroughput" "$match"
            ;;
        17)
            json_select statusData
            json_add_string "delay" "$match"
            ;;
        18)
            json_select statusData
            json_add_string "cinrCarrier" "$match"
            ;;
        19)
            json_select statusData
            json_add_string "cpu" "$match"
            ;;
        20)
            json_select statusData
            json_add_string "sendBytes" "$match"
            ;;
        21)
            json_select statusData
            json_add_string "receivedBytes" "$match"
            ;;
        22)
            json_select statusData
            json_add_string "normalOperationTime" "$match"
            ;;
        23)
            json_select statusData
            json_add_string "deviceCurrentTime" "$match"
            ;;
        24)
            json_select connectionInfo
            json_add_string "remoteIP" "$match"
            ;;
        25)
            json_select statusData
            json_add_string "remoteMacAddress" "$match"
            ;;
        esac
        json_select ..
        lineno=$((lineno + 1))
    done < $filename
        
}

foreach_server() {
    config_get enable $1 enable
    if $enable; then
        config_get server_ip $1 host
        config_get server_port $1 port
        config_get username $1 username
        config_get password $1 password
        config_get pubtopic $1 pubtopic

        mosquitto_pub -h $server_ip -p $server_port -m "$(json_dump)" -t $pubtopic -u $username -P $password
    fi
}

send_bridge_data() {
    
    #json_add_string "equipmentNumber" "$1"
    #mosquitto_pub -h $ip -p $port -m "$(json_dump)" -t $topic -u $username -P $password
    config_get ip $1 bridge_ip

    get_bridge_info_oid $ip
    json_add_string "equipmentNumber" "$1"

    config_foreach foreach_server server

}

excu_send_bridge()
{
    config_load idc_config
    config_foreach send_bridge_data bridge
}

excu_send_bridge